/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisualizadorDeEvento.java
 *
 * Created on 12/11/2011, 17:41:33
 */
package lp2.projeto.agenda;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;

import lp2.projeto.anotacao.Anotacao;
import lp2.projeto.anotacao.VisualizadorAnotacaoDialog;
import lp2.projeto.gerenciador.Evento;
import lp2.projeto.horario.Horario;
import lp2.projeto.horario.Mes;

/**
 *
 * @author Tercio de Melo
 */
public class VisualizadorDeEvento extends javax.swing.JDialog {

	private Evento evento;
	private List<Anotacao> anotacoes;
	private DefaultListModel anotacoesListModel;
	
    /** Creates new form VisualizadorDeEvento */
    public VisualizadorDeEvento(JDialog parent, boolean modal, Evento evento, boolean somente_leitura) {
    	super(parent, modal);
        this.evento = evento;
        
        initComponents(somente_leitura);
    }
    public VisualizadorDeEvento(JFrame parent, boolean modal, Evento evento, boolean somente_leitura) {
    	super(parent, modal);
        this.evento = evento;
        
        initComponents(somente_leitura);
    }
 

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(boolean somente_leitura) {

        nomeLabel = new javax.swing.JLabel();
        dataLabel = new javax.swing.JLabel();
        horarioLabel = new javax.swing.JLabel();
        tipoLabel = new javax.swing.JLabel();
        descricaoLabel = new javax.swing.JLabel();
        nomeField = new javax.swing.JTextField();
        tipoField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricaoArea = new javax.swing.JTextArea();
        salvarButton = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        anotacoesList = new javax.swing.JList();
        anotacoesLabel = new javax.swing.JLabel();
        visualizarAnotacaoButton = new javax.swing.JButton();
        criarAnotacaoButton = new javax.swing.JButton();
        removerAnotacaoButton = new javax.swing.JButton();
        diasList = new javax.swing.JComboBox();
        mesesList = new javax.swing.JComboBox();
        anosList = new javax.swing.JComboBox();
        horasList = new javax.swing.JComboBox();
        minutosList = new javax.swing.JComboBox();
        duracaoLabel = new javax.swing.JLabel();
        duracaoField = new javax.swing.JTextField();
        imagemButton = new javax.swing.JButton();
        somButton = new javax.swing.JButton();
        textoButton = new javax.swing.JButton();
        nomeAnotacaoField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        nomeLabel.setBackground(new java.awt.Color(255, 255, 255));
        nomeLabel.setText("Nome do Evento");

        dataLabel.setBackground(new java.awt.Color(255, 255, 255));
        dataLabel.setText("Data");

        horarioLabel.setBackground(new java.awt.Color(255, 255, 255));
        horarioLabel.setText("Horario");

        tipoLabel.setBackground(new java.awt.Color(255, 255, 255));
        tipoLabel.setText("Tipo");

        descricaoLabel.setBackground(new java.awt.Color(255, 255, 255));
        descricaoLabel.setText("Descricao");
        
        descricaoArea.setColumns(20);
        descricaoArea.setRows(5);
        jScrollPane1.setViewportView(descricaoArea);

        if (evento == null) {
            nomeField.setText("Nome do evento");
            tipoField.setText("Tipo do evento");
            descricaoArea.setText("Descricao do evento");
        } else {
            nomeField.setText(evento.getNome());
            tipoField.setText(evento.getTipo());
            descricaoArea.setText(evento.getDescricao());
        }

        salvarButton.setText("Salvar");
        salvarButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if (evento != null) {
						evento.setNome(nomeField.getText());
						evento.setDescricao(descricaoArea.getText());
						evento.setTipo(tipoField.getText());
						evento.setHorario(new Horario(anosList.getSelectedIndex()+1950, Mes.values()[mesesList.getSelectedIndex()],
								diasList.getSelectedIndex()+1, horasList.getSelectedIndex(), minutosList.getSelectedIndex()));
						evento.setDuracao(Integer.parseInt(duracaoField.getText()));
						evento.setAnotacoes(anotacoes);
					} else {
						Evento evento_novo = new Evento(nomeField.getText(), new Horario(anosList.getSelectedIndex()+1950, Mes.values()[mesesList.getSelectedIndex()],
								diasList.getSelectedIndex()+1, horasList.getSelectedIndex(), minutosList.getSelectedIndex()),
								descricaoArea.getText(), Integer.parseInt(duracaoField.getText()), tipoField.getText());
						evento_novo.setAnotacoes(anotacoes);
						AgendaGrafica.gerente.adicionaEvento(evento_novo);
					}
					
					AgendaGrafica.atualizarListas();
					dispose();
				} catch(Exception exception) {
					avisoJDialog aviso = new avisoJDialog("Algum dos campos estava invalido");
					aviso.setVisible(true);
				}
			}
		});

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

        if (evento == null) {
        	anotacoes = new ArrayList<Anotacao>();
        } else {
	        anotacoes = evento.getAnotacoes();
        }
        
        anotacoesListModel = new DefaultListModel();
        for (Anotacao anotacao : anotacoes)
        	anotacoesListModel.addElement(anotacao);
        
        anotacoesList.setModel(anotacoesListModel);
        jScrollPane2.setViewportView(anotacoesList);

        anotacoesLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        anotacoesLabel.setText("Anotacoes");

        criarAnotacaoButton.setText("Criar");
        criarAnotacaoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Anotacao nova_anotacao = new Anotacao(nomeAnotacaoField.getText());
				anotacoes.add(nova_anotacao);
				anotacoesListModel.addElement(nova_anotacao);
			}
		});

        removerAnotacaoButton.setText("Remover");
        removerAnotacaoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Anotacao selecionada = (Anotacao)anotacoesList.getSelectedValue();
				if (selecionada != null) {
					anotacoes.remove(selecionada);
					anotacoesListModel.removeElement(selecionada);
				}
			}
		});

        diasList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        mesesList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" }));

        anosList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035", "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050"  }));

        horasList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"  }));

        minutosList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));

        if (evento != null) {
        	diasList.setSelectedIndex(evento.getHorario().getDia()-1);
        	mesesList.setSelectedIndex(evento.getHorario().getMes().getNumero()-1);
        	anosList.setSelectedIndex(evento.getHorario().getAno()-1950);
        	horasList.setSelectedIndex(evento.getHorario().getHora());
        	minutosList.setSelectedIndex(evento.getHorario().getMinuto());
        }
        
        duracaoLabel.setText("Duracao");

        if (evento != null)
        	duracaoField.setText(String.format("%d", evento.getDuracao()));
        else
        	duracaoField.setText("Duracao do evento");
        
        final VisualizadorDeEvento eu_mesmo = this;
        
        imagemButton.setText("Imagem");
        imagemButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Anotacao selecionada = (Anotacao)anotacoesList.getSelectedValue();
				if (selecionada != null) {
					ImagemJDialog dialog = new ImagemJDialog(selecionada, eu_mesmo);
					dialog.setVisible(true);
				}
			}
        });
        
        somButton.setText("Som");
        somButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Anotacao selecionada = (Anotacao)anotacoesList.getSelectedValue();
				if (selecionada != null) {
					SomJDialog dialog = new SomJDialog(selecionada);
					dialog.setVisible(true);
;				}
			}
        });

        textoButton.setText("Texto");
        textoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Anotacao selecionada = (Anotacao)anotacoesList.getSelectedValue();
				if (selecionada != null) {
					TextoJDialog dialog = new TextoJDialog(selecionada);
					dialog.setVisible(true);
;				}
			}
        });        

        nomeAnotacaoField.setText("Nome da nova anotacao");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nomeLabel)
                    .addComponent(dataLabel)
                    .addComponent(tipoLabel)
                    .addComponent(horarioLabel)
                    .addComponent(duracaoLabel)
                    .addComponent(descricaoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nomeField, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(horasList, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(minutosList, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(diasList, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(mesesList, 0, 132, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(anosList, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(duracaoField, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                .addComponent(tipoField, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(imagemButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(somButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                                            .addComponent(textoButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(nomeAnotacaoField, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(criarAnotacaoButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(removerAnotacaoButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cancelarButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(salvarButton)))
                                .addGap(9, 9, 9))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(144, 144, 144)
                                .addComponent(anotacoesLabel)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomeLabel)
                    .addComponent(nomeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataLabel)
                    .addComponent(diasList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mesesList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anosList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anotacoesLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(horasList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(horarioLabel)
                    .addComponent(minutosList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(criarAnotacaoButton)
                    .addComponent(nomeAnotacaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipoLabel)
                            .addComponent(tipoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(imagemButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(duracaoLabel)
                            .addComponent(duracaoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(somButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(descricaoLabel)
                            .addComponent(textoButton)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(removerAnotacaoButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvarButton)
                    .addComponent(cancelarButton)))
        );

        pack();
        
        if (somente_leitura) {
        	nomeField.setEnabled(false);
        	anosList.setEnabled(false);
        	mesesList.setEnabled(false);
        	diasList.setEnabled(false);
        	horasList.setEnabled(false);
        	minutosList.setEnabled(false);
        	tipoField.setEnabled(false);
        	duracaoField.setEnabled(false);
        	descricaoArea.setEnabled(false);
        	criarAnotacaoButton.setEnabled(false);
        	removerAnotacaoButton.setEnabled(false);
        	imagemButton.setEnabled(false);
        	somButton.setEnabled(false);
        	textoButton.setEnabled(false);
        	nomeAnotacaoField.setEnabled(false);
        	salvarButton.setEnabled(false);
        }
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                VisualizadorDeEvento dialog = new VisualizadorDeEvento(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify
    protected javax.swing.JComboBox diasList;
    protected javax.swing.JComboBox mesesList;
    protected javax.swing.JComboBox anosList;
    private javax.swing.JLabel anotacoesLabel;
    private javax.swing.JList anotacoesList;
    private javax.swing.JButton criarAnotacaoButton;
    private javax.swing.JLabel dataLabel;
    private javax.swing.JTextArea descricaoArea;
    private javax.swing.JLabel descricaoLabel;
    private javax.swing.JLabel horarioLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nomeField;
    private javax.swing.JLabel nomeLabel;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JButton removerAnotacaoButton;
    private javax.swing.JButton salvarButton;
    private javax.swing.JTextField tipoField;
    private javax.swing.JLabel tipoLabel;
    private javax.swing.JButton visualizarAnotacaoButton;
    private javax.swing.JComboBox horasList;
    private javax.swing.JComboBox minutosList;
    private javax.swing.JLabel duracaoLabel;
    private javax.swing.JButton imagemButton;
    private javax.swing.JTextField nomeAnotacaoField;
    private javax.swing.JButton somButton;
    private javax.swing.JButton textoButton;
    private javax.swing.JTextField duracaoField;
    // End of variables declaration
   
}
